package com.loong.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.loong.framework.annotation.TransferDict;
import com.loong.framework.utils.CommonUtil;
import com.loong.system.entity.Dict;
import com.loong.system.mapper.DictMapper;
import com.loong.system.service.IDictService;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;

/**
 * @author 47349
 * @description 针对表【tb_dict(数据字典)】的数据库操作Service实现
 * @createDate 2024-01-18 03:35:09
 */
@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements IDictService {
  @Override
  public List<Dict> getDictByType(String type) {
    return this.lambdaQuery().eq(Dict::getType, type).list();
  }

  @Override
  public <E> void transfer(E e) {
    Map<String, Dict> sign2Dict =
        this.lambdaQuery()
            .select(Dict::getType, Dict::getValue, Dict::getDescription)
            .list()
            .stream()
            .collect(
                Collectors.toMap(key -> key.getType() + "_" + key.getValue(), Function.identity()));

    if (e instanceof Collection<?>) {
      ((Collection<?>) e)
          .forEach(
              i -> {
                Class<?> aClass = i.getClass();
                Field[] fieldItems = aClass.getDeclaredFields();

                if (CommonUtil.notNullOrEmpty(fieldItems)) {
                  for (Field fieldItem : fieldItems) {
                    TransferDict annotation = fieldItem.getAnnotation(TransferDict.class);
                    if (CommonUtil.isNullOrEmpty(annotation)) {
                      continue;
                    }

                    String type = annotation.type();
                    String field = annotation.field();

                    try {
                      Field f = aClass.getDeclaredField(field);
                      f.setAccessible(true);
                      String s = (String) f.get(i);

                      if (CommonUtil.isNullOrEmpty(s)) {
                        continue;
                      }

                      Dict dict = sign2Dict.get(type + "_" + s);
                      if (Objects.equals(dict.getValue(), s)) {
                        fieldItem.setAccessible(true);
                        fieldItem.set(i, dict.getDescription());
                      }

                    } catch (Exception ex) {
                      log.error("字典转换错误", ex);
                    }
                  }
                }
              });
    } else {
      Class<?> aClass = e.getClass();
      Field[] fieldItems = aClass.getDeclaredFields();
      if (CommonUtil.notNullOrEmpty(fieldItems)) {
        for (Field fieldItem : fieldItems) {
          TransferDict annotation = fieldItem.getAnnotation(TransferDict.class);
          if (CommonUtil.isNullOrEmpty(annotation)) {
            continue;
          }
          String type = annotation.type();
          String field = annotation.field();

          try {
            Field f = aClass.getDeclaredField(field);
            f.setAccessible(true);

            String s = (String) f.get(e);

            Dict dict = sign2Dict.get(type + "_" + s);
            if (Objects.equals(dict.getValue(), s)) {
              fieldItem.setAccessible(true);
              fieldItem.set(e, dict.getDescription());
            }

          } catch (Exception ex) {
            log.error("字典转换错误", ex);
          }
        }
      }
    }
  }

  @Override
  public <E> void transfer(Page<E> e) {
    transfer(e.getRecords());
  }
}
